package lib.control;

import lib.exception.ControlFactoryException;
import lib.exception.VideoControlException;
import lib.exception.VideoFileParseException;
import lib.logging.Logger;
import lib.model.VideoFile;
import settings.model.Settings;

import java.io.File;

public class VideoFileFactory {

    public static VideoFile get(final File file, final File basedir,
            final Settings settings, final String languagecode)
                    throws ControlFactoryException,
                    VideoFileParseException, VideoControlException {
        Logger.instance.trace("VideoFileFactory", "get", "");
        VideoFileControl vfc = VideoFileControlFactory.getController(file, basedir);
        VideoFile videoFile = null;
        if (vfc instanceof EpisodeFileControl) {
            if (languagecode.isEmpty()){
                videoFile = vfc.process(settings.getMappingList());
            }else{
                Logger.instance.log("Treating As Episode: "
                        + vfc.getVideoFile().getPath()
                        + File.separator
                        + vfc.getVideoFile().getFilename());
                
                videoFile = vfc.processWithSubtitles(settings.getMappingList(),
                        languagecode, settings.isOptionsShowExactMatch());
            }
        } else if (vfc instanceof MovieFileControl) {
            if (languagecode.isEmpty()){
                videoFile = vfc.process(settings.getMappingList());
            }else{
                Logger.instance.log("Treating As Movie: "
                        + vfc.getVideoFile().getPath()
                        + File.separator
                        + vfc.getVideoFile().getFilename());
                
                videoFile = vfc.processWithSubtitles(languagecode,
                        settings.isOptionsShowExactMatch());
            }
        }
        return videoFile;
    }

}
